<?php
require_once('../../includes/classes/core.php');

//----------------------------- ცვლადი

$agent	= $_REQUEST['agent'];
$name	= $_REQUEST['name'];
$queue	= $_REQUEST['queuet'];
$start_time = $_REQUEST['start_time'];
$end_time 	= $_REQUEST['end_time'];
$day = (strtotime($end_time)) -  (strtotime($start_time));
$day_format = ($day / (60*60*24)) + 1;


// ----------------------------------
$start_time = $db->convertDateTime($start_time);
$end_time = $db->convertDateTime($end_time);


if($_REQUEST['act'] =='check'){

		
//--------------------------- ნაპასუხები ზარები ოპერატორების მიხედვით

 	$ress =mysql_query("SELECT 	crst.`name` as name,
								COUNT(*) AS zarebi,
								(SELECT  COUNT(*)
									FROM `all`
									WHERE `all`.file_name LIKE '%დაუმ.შემომავ%'
									AND `all`.user2=crst.`name`
									AND `all`.`date` BETWEEN '$start_time' AND '$end_time'
								)AS daumushavebuli,
 								(SELECT  COUNT(*)
									FROM `all`
									WHERE `all`.file_name LIKE '%შემომავალი%'
									AND `all`.user2=crst.`name`
									AND `all`.`date` BETWEEN '$start_time' AND '$end_time'
								)AS damushavebuli,
								(SELECT  COUNT(*)
									FROM `all`
									WHERE `all`.file_name LIKE '%დაუმ.გამავ%'
									AND `all`.user1=crst.`name`
									AND `all`.`date` BETWEEN '$start_time' AND '$end_time'
								)AS daum_gamavali,
	 							(SELECT  SEC_TO_TIME(ROUND(SUM(TIMESTAMPDIFF(SECOND,task.planned_end_date,task.fact_end_date))/COUNT(*),0))
									FROM 	task
									left JOIN  crystal_users ON task.responsible_user_id=crystal_users.id
									WHERE crystal_users.`name`=crst.`name`
									AND  `task`.`date` >= '$start_time' 
									AND  `task`.`date` <= '$end_time' 
									AND  `task`.`status` = 3
									)AS shes_time,
								(SELECT SEC_TO_TIME(sum(TIMESTAMPDIFF(SECOND,
										IF(person_work_graphic.`start`< '$start_time','$start_time', person_work_graphic.`start`),
										IF(person_work_graphic.`end`  > '$end_time', '$end_time', person_work_graphic.`end`))))
									FROM 	person_work_graphic
									LEFT JOIN 	crystal_users ON person_work_graphic.person_id = crystal_users.id 										
									WHERE  	crystal_users.`name` = crst.`name` 
									AND  	person_work_graphic.actived=1 AND crystal_users.actived=1										
									AND   	(person_work_graphic.`start` >= '$start_time' OR 	person_work_graphic.`end` 	>= '$start_time')
									AND   	(person_work_graphic.`start` <= '$end_time' OR  person_work_graphic.`end` 	<= '$end_time')
									)AS gegmiuri,
								(SELECT COUNT(*)
									FROM 	task
									left JOIN  crystal_users ON task.responsible_user_id=crystal_users.id
									WHERE crystal_users.`name` = crst.`name`
									AND  `task`.`date` >= '$start_time' 
									AND  `task`.`date` <= '$end_time' 
									AND  `task`.`status` = 3) AS task_count,
								(SELECT  COUNT(*)
									FROM `all`
									WHERE `all`.file_name LIKE '%გამავალი%'
									AND `all`.user1=crst.`name`
									AND `all`.`date` BETWEEN '$start_time' AND '$end_time'
									
								)AS gamavali_damushavebuli,
						ROUND((COUNT(*)/(SELECT 	COUNT(*)
										FROM 	asterisk_incomming
										LEFT JOIN   	crystal_users AS crist ON asterisk_incomming.user_id = crist.id
										WHERE 	crist.`name` IN($agent)
										AND `asterisk_incomming`.`call_datetime` BETWEEN '$start_time' AND '$end_time'
										) *100), 2) AS `percent`,
								SEC_TO_TIME(SUM(asterisk_incomming.duration)) AS saubris_dro,
								SEC_TO_TIME(ROUND((SUM(asterisk_incomming.duration)/COUNT(*)),0)) AS sashualo,
								SEC_TO_TIME(MAX(asterisk_incomming.duration)) AS max_time,
								SEC_TO_TIME(MIN(asterisk_incomming.duration)) AS min_time
						FROM asterisk_incomming
						LEFT JOIN crystal_users AS crst ON asterisk_incomming.user_id = crst.id
						WHERE crst.`name` IN($agent)
						AND `asterisk_incomming`.`call_datetime` BETWEEN '$start_time' AND '$end_time'
						AND asterisk_incomming.duration>0
						GROUP BY crst.`name`
		 	  ");

while($row = mysql_fetch_assoc($ress)){
	$zarebi		 = $row[damushavebuli]+$row[daumushavebuli];
	$gamavali 	 = $row[daum_gamavali]+$row[gamavali_damushavebuli];
	$coeficienti = round($row[damushavebuli]/$zarebi,2);
	
	$data['page']['answer_call_by_queue'] .= '

                   	<tr>
					<td style="cursor:pointer;" id="name">'.$row[name].'</td>
					<td>'.$row[gegmiuri].' სთ</td>
					<td id="answear_dialog" style="cursor: pointer; text-decoration: underline;" user="'.$row[name].'">'.$zarebi.' ზარი</td>
					<td>'.$row[damushavebuli].' ზარი</td>
					<td id="undone_dialog" style="cursor: pointer; text-decoration: underline;" user1="'.$row[name].'">'.$row[daumushavebuli].' ზარი</td>
					<td>'.$row[percent].'%</td>
					<td>'.$row[saubris_dro].'</td>
					<td>'.$row[sashualo].'</td>
					<td>'.$row[max_time].'</td>
					<td>'.$row[min_time].'</td>
					<td>'.$coeficienti.'</td>
					<td>'.$row[task_count].'</td>
					<td>'.$row[shes_time].'</td>
					<td id="answear_dialog1" style="cursor: pointer; text-decoration: underline;" user2="'.$row[name].'">'.$gamavali.' ზარი</td>
					<td>'.$row[gamavali_damushavebuli].'ზარი</td>
					<td id="undone_dialog1" style="cursor: pointer; text-decoration: underline;" user3="'.$row[name].'">'.$row[daum_gamavali].' ზარი</td>
					</tr>

							';

}

//------------------------------//
}else if($_REQUEST['act'] =='answear_dialog_table'){
$data		= array('page' => array(
			'answear_dialog' => ''
	));
	$count = 		$_REQUEST['count'];
	$hidden = 		$_REQUEST['hidden'];
	$rResult = mysql_query("SELECT  `all`.date,
									`all`.date,
									`all`.phone,
									'2392200' AS source,
									`all`.user2,
									`all`.duration,
									`all`.file_name
							FROM `all`
							WHERE `all`.file_name LIKE '%დაუმ.შემომავ%'
							AND `all`.user2='$name'
							AND `all`.`date` BETWEEN '$start_time' AND '$end_time'
							UNION ALL
							SELECT  `all`.date,
									`all`.date,
									`all`.phone,
									'2392200' AS source,
									`all`.user2,
									`all`.duration,
									`all`.file_name
							FROM `all`
							WHERE `all`.file_name LIKE '%შემომავალი%'
							AND `all`.user2='$name'
							AND `all`.`date` BETWEEN '$start_time' AND '$end_time'");
	$data = array(
			"aaData"	=> array()
	);
		
	while ( $aRow = mysql_fetch_array( $rResult ) )
	{
		$row = array();
		for ( $i = 0 ; $i < $count ; $i++ )
		{
			/* General output */
			$row[] = $aRow[$i];
		}
		$data['aaData'][] = $row;
	}
	
	//------------------------------//
		
}elseif($_REQUEST['act'] =='undone_dialog_table'){
    $data		= array('page' => array(
        'answear_dialog' => ''
    ));
    $count = 		$_REQUEST['count'];
    $hidden = 		$_REQUEST['hidden'];
    
   $rResult = mysql_query("SELECT  `all`.date,
    								`all`.date,
									`all`.phone,
   									'2392200' AS source,
									`all`.user2,
    								`all`.duration,
									`all`.file_name
							FROM `all`
							WHERE `all`.file_name LIKE '%დაუმ.შემომავ%'
							AND `all`.user2='$name'
							AND `all`.`date` BETWEEN '$start_time' AND '$end_time'
							");
    $data = array(
        "aaData"	=> array()
    );
    
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
        $row = array();
        for ( $i = 0 ; $i < $count ; $i++ )
        {
            /* General output */
            $row[] = $aRow[$i];
        }
        $data['aaData'][] = $row;
    }
    
    //------------------------------//
    
}else if($_REQUEST['act'] =='answear_dialog_table1'){
$data		= array('page' => array(
			'answear_dialog' => ''
	));
	$count = 		$_REQUEST['count'];
	$hidden = 		$_REQUEST['hidden'];
	$rResult = mysql_query("SELECT  `all`.date,
									`all`.date,
									`all`.phone,
									`all`.user1,
									`all`.duration,
									`all`.file_name
							FROM `all`
							WHERE `all`.file_name LIKE '%დაუმ.გამავ%'
							AND `all`.user1='$name'
							AND `all`.`date` BETWEEN '$start_time' AND '$end_time'
							UNION all
							SELECT  `all`.date,
									`all`.date,
									`all`.phone,
									`all`.user1,
									`all`.duration,
									`all`.file_name
							FROM `all`
							WHERE `all`.file_name LIKE '%გამავალი%'
							AND `all`.user1='$name'
							AND `all`.`date` BETWEEN '$start_time' AND '$end_time'

			");
	$data = array(
			"aaData"	=> array()
	);
		
	while ( $aRow = mysql_fetch_array( $rResult ) )
	{
		$row = array();
		for ( $i = 0 ; $i < $count ; $i++ )
		{
			/* General output */
			$row[] = $aRow[$i];
		}
		$data['aaData'][] = $row;
	}
	
	//------------------------------//
		
}else if($_REQUEST['act'] =='undone_dialog_table1'){
    $data		= array('page' => array(
        'answear_dialog' => ''
    ));
    $count = 		$_REQUEST['count'];
    $hidden = 		$_REQUEST['hidden'];
    
    $rResult = mysql_query("SELECT  `all`.date,
    								`all`.date,
									`all`.phone,
									`all`.user1,
    								`all`.duration,
									`all`.file_name
							FROM `all`
							WHERE `all`.file_name LIKE '%დაუმ.გამავ%'
							AND `all`.user1='$name'
							AND `all`.`date` BETWEEN '$start_time' AND '$end_time'
							");
    $data = array(
        "aaData"	=> array()
    );
    
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
        $row = array();
        for ( $i = 0 ; $i < $count ; $i++ )
        {
            /* General output */
            $row[] = $aRow[$i];
        }
        $data['aaData'][] = $row;
    }

    }elseif($_REQUEST['act'] =='undone_dialog'){
    $data['page']['answear_dialog'] = '
															
													
												                <table class="display" id="example2" >
												                    <thead>
												                        <tr id="datatable_header">
												                            <th>ID</th>
												                            <th style="width: 180px;">თარიღი</th>
												                            <th style="width: 100px;">ადრესატი</th>
																			<th style="width: 100px;">ექსთენშენი</th>
    																		<th style="width: 100%;">ოპერატორი</th>
												                            <th style="width: 80px;">დრო</th>
												                            <th style="width: 100px;">ქმედება</th>
												                        </tr>
												                    </thead>
												                    <thead>
												                        <tr class="search_header">
												                            <th class="colum_hidden">
												                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" style=""/>
												                            </th>
												                            <th>
												                            	<input type="text" name="search_number" value="ფილტრი" class="search_init" style="">
																			</th>
												                            <th>
												                                <input type="text" name="search_date" value="ფილტრი" class="search_init" style="width: 100px;"/>
												                            </th>                            
												                            <th>
												                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 80px;" />
												                            </th>
												                            <th>
												                                <input type="text" name="search_phone" value="ფილტრი" class="search_init" style="width: 70px;"/>
												                            </th>
    																		<th>
												                                <input type="text" name="search_phone" value="ფილტრი" class="search_init" style="width: 70px;"/>
												                            </th>
												                            <th>
												                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 70px;" />
												                            </th>
																			<th>
												                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 80px;" />
												                            </th>
												                            
												                        </tr>
												                    </thead>
												                </table>
												        
						
													';
    
}else if($_REQUEST['act'] =='answear_dialog'){

				$data['page']['answear_dialog'] = '
															
													
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th style="width: 180px;">თარიღი</th>
                            <th style="width: 110px;">წყარო</th>
                            <th style="width: 110px;">ადრესატი</th>
							<th style="width: 100%;">ოპერატორი</th>
                            <th style="width: 80px;">დრო</th>
                            <th style="width: 90px;">ქმედება</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" style=""/>
                            </th>
                            <th>
                            	<input type="text" name="search_number" value="ფილტრი" class="search_init" style="">
							</th>
                            <th>
                                <input type="text" name="search_date" value="ფილტრი" class="search_init" style="width: 100px;"/>
                            </th>                            
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 80px;" />
                            </th>
                            <th>
                                <input type="text" name="search_phone" value="ფილტრი" class="search_init" style="width: 70px;"/>
                            </th>
							<th>
                                <input type="text" name="search_phone" value="ფილტრი" class="search_init" style="width: 70px;"/>
                            </th>
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 70px;" />
                            </th>
							<th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 80px;" />
                            </th>
                            
                        </tr>
                    </thead>
                </table>
        

	';
			
			
}elseif($_REQUEST['act'] =='undone_dialog1'){
    $data['page']['answear_dialog'] = '
															
													
												                <table class="display" id="example2">
												                    <thead>
												                        <tr id="datatable_header">
												                            <th>ID</th>
												                            <th style="width: 190px;">თარიღი</th>
												                            <th style="width: 120px;">ადრესატი</th>
																			<th style="width: 100%;">ოპერატორი</th>
												                            <th style="width: 80px;">დრო</th>
												                            <th style="width: 100px;">ქმედება</th>
												                        </tr>
												                    </thead>
												                    <thead>
												                        <tr class="search_header">
												                            <th class="colum_hidden">
												                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" style=""/>
												                            </th>
												                            <th>
												                            	<input type="text" name="search_number" value="ფილტრი" class="search_init" style="">
																			</th>
												                            <th>
												                                <input type="text" name="search_date" value="ფილტრი" class="search_init" style="width: 100px;"/>
												                            </th>                            
												                            <th>
												                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 80px;" />
												                            </th>
												                            <th>
												                                <input type="text" name="search_phone" value="ფილტრი" class="search_init" style="width: 70px;"/>
												                            </th>
												                            <th>
												                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 70px;" />
												                            </th>
																			<th>
												                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 80px;" />
												                            </th>
												                            
												                        </tr>
												                    </thead>
												                </table>
												        
						
													';
}else if($_REQUEST['act'] =='answear_dialog1'){

				$data['page']['answear_dialog'] = '
															
													
                <table class="display" id="example2_1">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th style="width: 190px;">თარიღი</th>
                            <th style="width: 120px;">ადრესატი</th>
							<th style="width: 100%;">ოპერატორი</th>
                            <th style="width: 80px;">დრო</th>
                            <th style="width: 100px;">ქმედება</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" style=""/>
                            </th>
                            <th>
                            	<input type="text" name="search_number" value="ფილტრი" class="search_init" style="">
							</th>
                            <th>
                                <input type="text" name="search_date" value="ფილტრი" class="search_init" style="width: 100px;"/>
                            </th>                            
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 80px;" />
                            </th>
                            <th>
                                <input type="text" name="search_phone" value="ფილტრი" class="search_init" style="width: 70px;"/>
                            </th>
                            <th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 70px;" />
                            </th>
							<th>
                                <input type="text" name="search_category" value="ფილტრი" class="search_init" style="width: 80px;" />
                            </th>
                            
                        </tr>
                    </thead>
                </table>
        

	';
			
			
}
echo json_encode($data);
?>